import { ElLoading } from 'element-plus'

let loadingInstance = null
let loadingCount = 0

// 全局loading管理
export const loading = {
    // 显示loading
    show(text = '加载中...') {
        if (loadingCount === 0) {
            loadingInstance = ElLoading.service({
                lock: true,
                text: text,
                background: 'rgba(0, 0, 0, 0.7)'
            })
        }
        loadingCount++
    },

    // 隐藏loading
    hide() {
        loadingCount--
        if (loadingCount <= 0) {
            if (loadingInstance) {
                loadingInstance.close()
                loadingInstance = null
            }
            loadingCount = 0
        }
    },

    // 重置loading状态
    reset() {
        if (loadingInstance) {
            loadingInstance.close()
            loadingInstance = null
        }
        loadingCount = 0
    }
}

export default loading 